/**
 * 给出一个无重叠的 ，按照区间起始端点排序的区间列表。
 * <p>
 * 在列表中插入一个新的区间，你需要确保列表中的区间仍然有序且不重叠（如果有必要的话，可以合并区间）。
 * <p>
 *  
 * <p>
 * 示例 1：
 * <p>
 * 输入：intervals = [[1,3],[6,9]], newInterval = [2,5]
 * 输出：[[1,5],[6,9]]
 * 示例 2：
 * <p>
 * 输入：intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
 * 输出：[[1,2],[3,10],[12,16]]
 * 解释：这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。
 *  
 */
class Solution {

    public static void main(String[] args) {
        /*int[][] intervals = new int[2][2];
        intervals[0] = new int[]{1, 3};
        intervals[1] = new int[]{6, 9};
        int[] newInterval = new int[]{2, 5};*/
        /*int[][] intervals = new int[5][2];
        intervals[0] = new int[]{1, 2};
        intervals[1] = new int[]{3, 5};
        intervals[2] = new int[]{6, 7};
        intervals[3] = new int[]{8, 10};
        intervals[4] = new int[]{12, 16};
        int[] newInterval = new int[]{4, 8};*/
        int[][] intervals = new int[1][2];
        intervals[0] = new int[]{1, 5};
        int[] newInterval = new int[]{0, 0};
        /*int[][] intervals = new int[2][2];
        intervals[0] = new int[]{3, 5};
        intervals[1] = new int[]{12, 15};
        int[] newInterval = new int[]{6, 6};*/
        System.out.println(insert(intervals, newInterval).length);
    }

    public static int[][] insert(int[][] intervals, int[] newInterval) {
        if (intervals.length == 0) {
            int[][] result = new int[1][2];
            result[0] = newInterval;
            return result;
        }
        int start = -1, end = -1;
        int startNum = -1, endNum = -1;
        for (int i = 0; i < intervals.length; i++) {
            // 优先找需要合并的start
            if (start == -1 && intervals[i][1] >= newInterval[0]) {
                start = i;
                startNum = Math.min(newInterval[0], intervals[i][0]);
                // 判断end
                if (intervals[i][0] > newInterval[1]) {
                    // 如果夹在两个中间
                    int[][] result = new int[intervals.length + 1][2];
                    System.arraycopy(intervals, 0, result, 0, i);
                    result[i] = newInterval;
                    System.arraycopy(intervals, i, result, i + 1, intervals.length - i);
                    return result;
                } else if (intervals[i][1] >= newInterval[1]) {
                    end = i;
                    endNum = intervals[i][1];
                    break;
                }
            } else {
                // 如果已经找到了start，找end
                if (intervals[i][0] > newInterval[1]) {
                    end = i - 1;
                    endNum = newInterval[1];
                    break;
                } else if (intervals[i][1] >= newInterval[1]) {
                    end = i;
                    endNum = intervals[i][1];
                    break;
                }
            }
        }
        if (start == -1) {
            // 如果连开头都没有匹配到则直接拼接到后面
            int[][] result = new int[intervals.length + 1][2];
            System.arraycopy(intervals, 0, result, 0, intervals.length);
            result[intervals.length] = newInterval;
            return result;
        }
        if (end == -1) {
            // 如果结尾没匹配到，则手动加下结尾
            end = intervals.length - 1;
            endNum = newInterval[1];
        }
        // 根据情况进行合并
        if (start == end) {
            intervals[start][0] = startNum;
            intervals[start][1] = endNum;
            return intervals;
        } else {
            int[][] result = new int[intervals.length - (end - start)][2];
            if (start > 0) {
                System.arraycopy(intervals, 0, result, 0, start);
            }
            result[start][0] = startNum;
            result[start][1] = endNum;
            if (end < intervals.length - 1) {
                System.arraycopy(intervals, end + 1, result, start + 1, intervals.length - 1 - end);
            }
            return result;
        }
    }

}